package com.star.es.service.impl;

import java.util.Map;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.star.es.entity.ESQuery;
import com.star.es.entity.ESSearchBuilder;
import com.star.es.entity.QueryParam;
import com.star.es.service.TermQuery;

/**
 * 词条查询实现
 * @author Administrator
 *
 */
@Service
public class TermQueryImpl implements TermQuery {

	private static final String FIELD_SPLIT = ",";
	@Autowired
	private TransportClient client;

	@Override
	public SearchResponse termsQuery(QueryParam queryParam) {
		ESQuery esQuery = queryParam.convertESQuery();
		Map<String, Object> queryMap = queryParam.getQuery();
		queryMap.keySet().forEach(key -> {
			esQuery.setQuery(QueryBuilders.termsQuery(key, queryMap.get(key).toString().split(FIELD_SPLIT)));
		});
		return new ESSearchBuilder(client, esQuery).build();
	}

}
